home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
.net (Poland) 2000 September
/
Magazyn_Net_09_2000 (CDA).iso
/
internet
/
Winproxy30.exe
/
w
/
_SETUP.1
/
ProxyLog.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-12-03
|
7KB
|
311 lines
// This is the proxy log application that will
// recieve messages from the proxy and log them.
// What happens is this guy listens on a port at an IP address
// and when the Proxy Server starts up, it connects to this and
// sends all of its debug messages here
#define PROXYLOG_VERSION "0.93"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
int Port=8000;
HANDLE StdIn;
void GetKey( void )
{
printf( "\nPress any key to continue" );
getch();
printf("\n");
}
int Wait( void )
{
INPUT_RECORD Input;
long NumRead=0;
// This makes the app stop using up all system time - Kevin
Sleep( 1 );
GetNumberOfConsoleInputEvents( StdIn, &NumRead );
if( NumRead == 0 )
return( TRUE );
ReadConsoleInput( StdIn, &Input, 1, &NumRead );
if( !NumRead )
return( TRUE );
if( Input.EventType == KEY_EVENT && Input.Event.KeyEvent.bKeyDown )
{
if( Input.Event.KeyEvent.uChar.AsciiChar == 'x'
|| Input.Event.KeyEvent.uChar.AsciiChar == 'X'
)
return( FALSE );
}
return( TRUE );
}
void main( int argc, char **argv, char **argp )
{
WSADATA SocketData;
SOCKET NewSock=INVALID_SOCKET, MySock=INVALID_SOCKET;
fd_set Set;
char *FileName=NULL;
int FileHandle=(-1);
printf("\nWinProxy logging application V"PROXYLOG_VERSION"\n\n" );
StdIn = GetStdHandle( STD_INPUT_HANDLE );
if( argc <= 1 )
{
printf("\nNo port number specified, listening for a logging connection on port %d\n\n", Port );
}
else
{
argc--;
argv++;
while( argc )
{
char *String = *argv;
int NewPort;
if( *String == '/' )
{
switch( String[1] )
{
case 'P':
case 'p':
NewPort = atoi( &String[3] );
if( Port <= 1024 || Port >= 32000 )
{
printf( "\nThe port number must be between 1024 and 32000\n\
Using port %d instead.\n\n", Port );
}
else
Port = NewPort;
break;
case 'F':
case 'f':
FileName = &String[3];
printf("\nSending output to file %s\n", FileName );
break;
default:
printf("\nUsage:\n\n\
ProxyLog [/P:portnum] [/F:filename]\n\n\
portnum must be between 1024 and 32,000 and designates the TCP/IP port\n\
where the application will listen for a connection. If no port\n\
is specified, then port %d will be used.\n\
\n\
filename optionally specifies a filename that will be used for logging.\n\
All logged data is saved to this file.\n\
\n\
", Port );
return;
}
}
argc--;
argv++;
}
}
if( FileName )
{
FileHandle = open( FileName, _O_APPEND|_O_CREAT|_O_WRONLY, _S_IREAD|_S_IWRITE );
if( FileHandle == (-1) )
{
printf("\nUnable to open file %d for writing\n", FileName );
GetKey();
return;
}
}
if( WSAStartup( 0x0101, &SocketData ) )
{
printf("Unable to start up Winsock!!\n\n" );
GetKey();
return;
}
if( SocketData.wVersion < 0x0101 )
{
printf("Old version of Winsock present!\nVersion 1.1 is required!\n" );
GetKey();
goto AllDone;
}
ListenAgain:
MySock = socket( PF_INET, SOCK_STREAM, 0 );
if( MySock == INVALID_SOCKET )
{
printf("Unable to create socket\n\n");
GetKey();
goto AllDone;
}
{
SOCKADDR_IN sin;
sin.sin_family = AF_INET;
sin.sin_port = htons( (u_short)Port );
sin.sin_addr.s_addr = 0; // Don't bind a any card in particular!
if( bind( MySock, (LPSOCKADDR)&sin, sizeof( sin ) ) )
{
printf("Unable to bind socket to port %d\n\n", Port );
GetKey();
goto AllDone;
}
}
// now the sokcet is ready and bound... listen for connection!
if( listen( MySock, 5 ) )
{
printf("WINSOCK error! Unable to listen for connection on socket.\n\n" );
GetKey();
goto AllDone;
}
printf("Waiting for a connection\nPress 'X' to abort\n\n" );
// now just wait for a connection!
{
struct sockaddr Address;
int AddrLen;
struct timeval timeout;
printf(" " );
while( 1 )
{
static char Spin[]="-/|\\";
static int CurChar=0;
static int Count=0;
int Ready;
FD_ZERO( &Set );
FD_SET( MySock, &Set );
timeout.tv_sec = 0;
timeout.tv_usec = 0;
Ready = select( 0, &Set, NULL, NULL, &timeout );
if( Ready )
{
AddrLen = sizeof( Address );
NewSock = accept( MySock, &Address, &AddrLen );
break;
}
Count++;
if( Count >= 1000 )
{
Count = 0;
printf("\b%c", Spin[CurChar++] );
if( Spin[CurChar] == '\0' )
CurChar = 0;
}
if( !Wait() )
goto AllDone;
}
if( NewSock == INVALID_SOCKET )
{
printf("Failed to connect to proxy server\n\n" );
goto AllDone;
}
printf("Connected!!\n" );
// Don't need this guy any more!
closesocket( MySock );
MySock = INVALID_SOCKET;
}
// OK! now we are connected and ready to go!
// Just read the input from the socket and put it on
// the screen!
while( Wait() )
{
static struct fd_set ReadSet;
struct timeval timeout;
int HaveData;
FD_ZERO( &Set );
FD_SET( NewSock, &Set );
timeout.tv_sec = 0;
timeout.tv_usec = 0;
HaveData = select( 0, &Set, NULL, NULL, &timeout );
if( HaveData == SOCKET_ERROR ) // If select had an error... get out of here...
break;
else
if( HaveData )
{
static char Buffer[4096];
int NumRecieved, i;
NumRecieved = recv( NewSock, Buffer, sizeof( Buffer ), 0 );
if( NumRecieved == SOCKET_ERROR
|| NumRecieved == 0
)
{
printf("Socket error recieving. WinProxy may have closed the socket\n");
closesocket( NewSock );
NewSock = INVALID_SOCKET;
break;
}
else
for( i = 0; i < NumRecieved; i++ )
putchar( Buffer[i] );
if( NumRecieved && FileHandle != (-1) )
{
write( FileHandle, Buffer, NumRecieved );
write( FileHandle, Buffer, 0 ); // flush the buffers.
}
}
}
if( NewSock != INVALID_SOCKET )
{
closesocket( NewSock );
NewSock = INVALID_SOCKET;
}
printf("Logging application completed successfully\n\
Waiting for another connection.\n" );
goto ListenAgain; // yeah... can you believe it! oh, well... the whiles get nested
// too deep otherwise... and I can't break out very well. It is
// just a sample.
AllDone:
if( MySock != INVALID_SOCKET )
closesocket( MySock );
WSACleanup();
if( FileHandle != (-1) )
close( FileHandle );
}